"use strict"
/**
 * 省级补助记录查看
 * sy
 * @param id
 */
function provinceVaccineView(id,auditId){
    layer.open({
        type : 2,
        fix : true,
        skin : 'self-def', /* 自定义按钮样式 */
        anim : '5', /* 动画效果 */
        move : [ '.layui-layer-title', true ],
        title : [ '补助详细信息', 'font-size:20px;font-weight:bold;'],
        offset : [ '30px' ],
        area : [ '85%', '80%' ],
        maxmin : false,
        closeBtn : 1,
        content:  path + '/bus/vaccine/vaccineView?id='+id,
        btn : ['一键驳回','关闭'], /* 自定义按钮事件 */
        btn1 : function(index, layero) {
            provinceReject(id,auditId);
           /* var innerObj = window[layero.find('iframe')[0]['name']];
            innerObj.$("#inputForm").submit();*/
            return false;
        },
        btn2 : function(index, layero) {
            layer.close(index);
        }
    });
}

/**
 * 省级一键驳回
 * sy
 */
function provinceReject(id,auditId){
    layer.open({
        type : 2,
        fix : true,
        skin : 'self-def', /* 自定义按钮样式 */
        anim : '5', /* 动画效果 */
        move : [ '.layui-layer-title', true ],
        title : [ '系统提示', 'font-size:20px;font-weight:bold;'],
        offset : [ '30px' ],
        area : [ '40%', '50%' ],
        maxmin : false,
        closeBtn : 1,
        content:  path + '/bus/vaccine/provinceRejectForm?id='+id+'&auditId='+auditId,
        btn : ['确定'], /* 自定义按钮事件 */
        // btn1 : function(index, layero) {
        //     layer.close(index);
        // },
        btn1 : function(index, layero) {
            var innerObj = window[layero.find('iframe')[0]['name']];
            innerObj.$("#inputForm").submit();
            return false;
        }
    });
}

/**
 * 省级补助记录查看
 * sy
 * @param id
 */
function provinceRejectView(id){
    layer.open({
        type : 2,
        fix : true,
        title : [ '补助详细信息', 'font-size:20px;font-weight:bold;'],
     //   skin : 'self-def', /* 自定义按钮样式 */
        anim : '5', /* 动画效果 */
        scrollbar:false,
        move : [ '.layui-layer-title', true ],
        offset : [ '30px' ],
        area : [ '85%', '80%' ],
        maxmin : false,
        closeBtn : 1,
        content:  path + '/bus/vaccine/vaccineView?id='+id,
        btn : ['关闭'], /* 自定义按钮事件 */
    });
}

/**
 * 获取发票照片
 * sy
 * @param tblId
 * @param tblName
 */
function invoicePhotoView(tblId,tblName,type){
    $.ajax({
        url: ctx+"/sys/attachment/invoicePhotoView",
        type: "post",
        data: { "tblId": tblId, "tblName": tblName ,"type":type},
        async : false,
        success : function(href) {
            if(href!=''){
             //    layer.open({
             //        type: 1,
             //        title: '查看',
             // //       offset: ['50px', '150px'],/* 垂直水平居中 */
             //        area: ['50%', '65%'],
             //        content: '<img style="width: 100%;height: 100%" src="' + href + '" </img>'
             //    });
                layer.open({
                    type: 2,
                    title: ['图片预览', 'font-family: Arial Negreta; font-size:20px; font-weight: 700; color:#33A5E7;'],
                    shade: 0.8,
                    offset: 'auto',
                    closeBtn: 1,
                    shadeClose: true,
                    area: ['85%', '90%'],
                    content: ctx +  '/bus/vaccine/imageView?imagePath='+href,
                    btn : [ "关闭"],
                });
            }else{
                top.$.jBox.tip("暂无图片", "info");
            }
        }
    });
}

function checkPage(id,auditId,flag){
    var url=ctx+"/bus/vaccine/updateVaccineStatus";
    var url1=ctx+"/bus/vaccine/updateVaccineStatus?auditId="+auditId+"&updateFlag=2&id="+id+"&auditOpinion=";
    if(flag=='0'){
        layer.open({
            type :2,
            title : ['补助详细信息',
                'font-family: Arial Negreta; font-size:20px; font-weight: 700; color:#33A5E7;'],
            skin : "self-def", /* 自定义按钮样式 */
            anim : "5", /* 动画效果 */
            area : [ "85%", "80%" ],
            content :ctx +  '/bus/vaccine/vaccineView?id='+id,
            btn : [ "通过", "驳回" ],
            yes : function(index, layero) {
                layer.confirm  ('确定要通过这条补助申请吗？',{icon: 3,title:'系统提示',
                    btn:'确定',},function(){
         //          audit(id,auditId,'1');
                    $.ajax({
                        type: "post",
                        url:url,
                        data: {auditId:auditId,updateFlag:'1',id:id},
                        success:function(data) {
                            layer.msg("审核成功！", {icon: 1, time: 1000}, function (){
                                window.location.reload();
                            })
                        }
                    });
                });
            },
            btn2 : function(index, layero) {
                prompt("系统提示","驳回原因",url1,'function');
                return false;
                // layer.prompt({
                //     formType:2,
                //     title: '请输入驳回原因',
                //     maxlength:90,
                //     area: ['552px', '118px']
                // }, function(value, index, elem){
                //     audit(id,auditId,'2',value);
                //     layer.close(index);
                // });
            }
        });
    }
    if(flag=='1'){
        layer.open({
            type :2,
            title : ['补助详细信息',
                'font-family: Arial Negreta; font-size:20px; font-weight: 700; color:#33A5E7;'],
            skin : "self-def", /* 自定义按钮样式 */
            anim : "5", /* 动画效果 */
            area : [ "85%", "80%" ],
            content : ctx +'/bus/vaccine/vaccineView?id='+id,
            btn : ['关闭']
        });
    }
    // var audit = function (id,auditId,flag,opinion=''){
    //     $.ajax({
    //         type: "post",
    //         url:ctx+"/bus/vaccine/updateVaccineStatus",
    //         data: {auditId:auditId,updateFlag:flag,id:id,auditOpinion:opinion},
    //         success:function(data) {
    //             layer.msg("1"== data ? ('1' == flag ? '审核成功':'驳回成功') :"审核失败！", {icon: 1, time: 1000}, function (){
    //                 //Todo 火狐浏览器刷新页面报错 待解决
    //                 window.location.reload();
    //             })
    //         }
    //     });
    // }
}
function prompt(title, lable, href, closed){
    top.$.jBox("<div class='form-search' style='padding:20px;text-align:center;width:=800px;height:118px;'>" + lable + "：<textarea id='txt' name='txt' maxlength='90' style='height:118px;width: 200px'></textarea></div>", {
        title: title, submit: function (v, h, f){
            if (f.txt == '') {
                top.$.jBox.tip("请输入" + lable + "。", 'error');
                return false;
            }
            if (typeof href == 'function') {
                href();
            }else{
                 resetTip();
                location = href + encodeURIComponent(f.txt);
            }
        },closed:function(){
            if (typeof closed == 'function') {
                closed();
            }
        }});
    return false;
}

/**
 * @author Zhang Xingliang
 * @description 预览照片 优化代码传入img对象本身，不传入连接，减小html代码大小
 * @date 15:34 2019/8/11
 **/
// function viewPhoto(o){
//     if('' != o.src){
//         var src = o.src;
//         src.lastIndexOf("?imageView2") > 0 && (src = src.substr(0, src.lastIndexOf("?imageView2")));
//         layer.open({
//             type: 1,
//             title: '查看',
//             //       offset: ['50px', '150px'],/* 垂直水平居中 */
//             area: ['65%', '80%'],
//             content: '<img style="width: 100%;height: 100%" src="' + src + '" </img>'
//         });
//     }else{
//         top.$.jBox.tip("暂无图片", "info");
//     }
// }

function viewPhoto(o){
    if('' != o.src){
        var src = o.src;
        src.lastIndexOf("?imageView2") > 0 && (src = src.substr(0, src.lastIndexOf("?imageView2")));
        parent.layer.open({
            type: 2,
            title: ['图片预览', 'font-family: Arial Negreta; font-size:20px; font-weight: 700; color:#33A5E7;'],
            shade: 0.8,
            offset: 'auto',
            closeBtn: 1,
            shadeClose: true,
            area: ['85%', '90%'],
            content: ctx +  '/bus/vaccine/imageView?imagePath='+src,
            btn : [ "关闭"],

        });
    }else{
        top.$.jBox.tip("暂无图片", "info");
    }
}

/**
*@Title: checkInvoice
*@Author: zyj
*@Date: 2019/8/11 15:48
*@Description: 发票验真
*@Param:
*@Return:
*/
function checkInvoice(id){
    var vaccineImg = $("#invoice").attr('filePath');
    parent.layer.open({
        type: 2,
        title: ['发票真伪校验',
            'font-family: Arial Negreta; font-size:20px; font-weight: 700; color:#33A5E7;'],
        area: ['100%', '100%'],
        content: ctx +  '/bus/vaccine/vaccineViewCheck?id='+id+'&vaccineImg='+vaccineImg,
        btn : [ "确定"],
        yes : function(index, layero) {
            var innerObj = layero.find("iframe")[0].contentWindow;
            var data={
                id:innerObj.$('#id').val(),
                state:innerObj.$('#state').val(),
                invoiceCode:innerObj.$('#invoiceCode').val(),
                invoiceNo:innerObj.$('#invoiceNo').val(),
                invoiceDate:innerObj.$('#invoiceDate').val(),
                totalAmount:innerObj.$('#totalAmount').val(),
                checkCode:innerObj.$('#checkCode').val(),
                invoiceType:innerObj.$('input:radio:checked').val(),
            };
            checkReal(1,data,function(res){
                if('success' == res.data.msg){
                    parent.layer.close(index);
                }
            });//手动验证
        }
    });
}

/**
 * @author Zhang Xingliang
 * @description 发票鉴别真伪并返回所有数据
 * @date 14:27 2019/7/23 0023
 * @param
 * @return
 **/
function checkReal(type,data,callback) {
    if(!validateRequire(data)){
        return;
    }
    $.ajax({
        type: "POST",
        url: ctx + "/bus/vaccine/checkInvoiceReal",
        data: data,
        dataType:"json",
        success: function (res) {
            message(res.data.msg);
            if ('success' == res.data.msg) {
                window.location.reload();
            }
            callback(res);
        }
    });
    function message(k){
       if( 0==type )return;
        var msg = {
            success:'此发票真实有效！',
            fail:'发票识别失败(无效发票或不清晰)，请核对信息重新校验！',
            invalid:'发票已经作废，无法作为补助凭证！',
            exist:'您上传的发票已存在，请上传正确的发票！',
            existEdit:'您填写的发票编号已存在，请填写正确的发票编号！',
            "20":'您上传的发票今日已超出查验次数，请明日在试！',
            "1014":'您上传的发票是今日发票，发票当日无法查验，请明日在试！',
            "invoiceNo":'发票编号不能为空！',
            "invoiceDate":'发票日期不能为空！',
            "invoiceCode":'发票代码不能为空！',
            "totalAmount":'发票金额不能为空！',
            "checkCode":'请输入发票校验码后6位！'
        }
        parent.layer.msg(msg[k], {
            icon :  k == 'success' ? 1 : 2 ,
            time : 2000
        });
    }
    function validateRequire(data){
        if(isToday(data.invoiceDate)){
            message('1014');
            return false;
        }
        if('' == data.invoiceNo){
            message('invoiceNo');
            return false;
        }
        if('' == data.invoiceDate){
            message('invoiceDate');
            return false;
        }
        if('' == data.invoiceCode){
            message('invoiceCode');
            return false;
        }
        if('0' == data.invoiceType && '' == data.totalAmount){
            message('totalAmount');
            return false;
        }
        if('1' == data.invoiceType && ('' == data.checkCode || data.checkCode.length != 6)){
            message('checkCode');
            return false;
        }
        return true;
    }
}

function isToday(str){
    var d = new Date(str.replace(/-/g,"/"));
    var todaysDate = new Date();
    if(d.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0)){
        return true;
    } else {
        return false;
    }
}

function changeRadio(o){
    if('1' == o.value){//专票
        $('.invoiceType.checkCode').removeClass('dHide');
        $('.invoiceType.totalAmount').addClass('dHide');
    }else{
        $('.invoiceType.totalAmount').removeClass('dHide');
        $('.invoiceType.checkCode').addClass('dHide');
    }
}

function showImg(o) {
    var src = $(o).attr('imgSrc');
    var content = '';
    content += '<p style="font-size: 16px; margin-left:15px;margin-top: 5px">' +
        '如图右侧所示，请核对发票代码、发票编号、开票日期、不含税金额、校验码后六位与系统识别的信息是否一致。</p>';
    content += '<img style="width: 100%;height: 100%" src=' + src + ' </img>';
    layer.open({
        type: 1,
        title: ['如何核对',
            'font-family: Arial Negreta; font-size:20px; font-weight: 700; color:#33A5E7;'],
        area: ['90%', '95%'],
        content: content
    });
}